package com.example.tourpal;

import java.text.NumberFormat;

import android.app.Activity;
import android.content.Intent;
import android.os.Bundle;
import android.view.View;
import android.view.View.OnClickListener;
import android.widget.Button;
import android.widget.DatePicker;

public class SpotDateSetActivity extends Activity {

	public static final int RESULT_BACK = 11;
	private static final String SLASH = "/";
	private static final String UNDER_LINE = "-";
	private DatePicker dateFrom;
	private DatePicker dateTo;

	private Button dateConfirm;

	private int spotId;

	@Override
	protected void onCreate(Bundle savedInstanceState) {
		// TODO Auto-generated method stub
		super.onCreate(savedInstanceState);

		setContentView(R.layout.date_set_viewpage);
		spotId = getIntent().getIntExtra("spotId", 0);

		dateFrom = (DatePicker) findViewById(R.id.spotDateFrome);
		dateTo = (DatePicker) findViewById(R.id.spotDateTo);

		dateConfirm = (Button) findViewById(R.id.spotDateConfirm);
	}

	@Override
	protected void onResume() {
		super.onResume();

		dateConfirm.setOnClickListener(new OnClickListener() {

			@Override
			public void onClick(View v) {
				NumberFormat nf = NumberFormat.getInstance();
				nf.setMinimumIntegerDigits(2);
				nf.setMaximumIntegerDigits(2);

				String spotDateFrom = String.valueOf(dateFrom.getYear())
						+ SLASH + nf.format(dateFrom.getMonth() + 1) + SLASH
						+ nf.format(dateFrom.getDayOfMonth());
				String spotDateTo = String.valueOf(dateTo.getYear()) + SLASH
						+ nf.format(dateTo.getMonth() + 1) + SLASH
						+ nf.format(dateTo.getDayOfMonth());

				Intent intent = new Intent();
				intent.putExtra("spotId", spotId);
				intent.putExtra("spotSet", spotDateFrom + UNDER_LINE
						+ spotDateTo);
				setResult(RESULT_BACK, intent);
				finish();
			}
		});
	}
}
